<?php
namespace app\home\controller;
use app\home\controller\Common;
use app\home\model\Api as apiModel;
use think\Db;
class Api extends Common
{
    private $return = array(
        'errcode' => 1,
        'errmsg'    => ''
        );

    function haha(){
        echo 111;
    }

    function test(){

        // $headers = array(
        //     // "POST /api/values/get_signature HTTP/1.1",
        //     "Host: 10.46.12.150:8820",
        //     "Connection: keep-alive",
        //     "Pragma: no-cache",
        //     "Cache-Control: no-cache",
        //     "DNT: 1",
        //     "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
        //     "Content-type: application/json",
        //     "Accept: */*",
        //     "Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm",
        //     "Accept-Encoding: gzip, deflate",            
        //     "Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8",
        //     // "Accept-Language: zh-CN",
        //     // "Origin: https://test.api.taichi.iotourism.com",
        //     // "User-Agent: ApiPOST Runtime +https://www.apipost.cn"
        // );
        // $url = 'http://10.46.12.150:8820/api/values/patient_list';
        // $request = '{"requestHead":{"account_id":"test01","method":"patient_list","timestamp":"20201222101000","version":"1.0","sign":"6553be2e3a2aa63ec1616486b57a6f1e"},"requestBody":{"page":"1","page_size":"3","starttime":"2020-01-01","endtime":"2020-02-01","dept_id":"13002"}}';
        // $response = post_url($url, $request, $headers);
        // print_r($response);exit;

        // $url = "http://10.46.12.150:8820/api/values/patient_list";
        // $request = '{a:1}';
        // $response = post_url($url, $request);
        // print_r($response);exit;

        $apiModel = new apiModel();
        // syncPatientList($page="1", $page_size="20", $department_code='', $starttime='', $endtime='')
        $endtime = date("Y-m-d");
        $apiModel->syncPatientList("1", "20", "13006", "2020-01-01", $endtime);
    }

    function test2(){
        // $apiModel = new apiModel();
        $requestData['patient_id'] = '7812376';
        $requestData['department'] = '13006';
        $requestData['location'] = '大门口';
        $requestData['rfid'] = 'e2000019491302161930bcd0';
        $requestData['equipment'] = '2';
        // addlog($requestData, 'syncWarning');
        dump($requestData);exit;
        // $apiModel->syncWarning($requestData);
    }

    function tcp_rfid(){
        $apiModel = new apiModel();
        addlog(input('get.'), 'tcp_rfid');
        if( empty(input('get.data')) ){
            // return false;
            exit;
        }
        $inputData = json_decode(base64_decode(input('get.data')), true);
        addlog($inputData, 'tcp_rfid');
        if(!is_array($inputData) || empty($inputData)){
            // return false;
            exit;
        }
        foreach($inputData as $key=>$val){
            // $rfid = trim(input('get.xinpian'));
            // $addr = trim(input('get.addr'));
            $rfid = trim($val['xinpian']);
            $addr = trim($val['addr']);
            if(!empty($rfid) && !empty($addr)){
                //签到
                // $sign = Db::name('tcp_rfid')->field('id')->where(array('xinpian'=>$rfid,'addr'=>$addr))->order('id desc')->find();
                // if(!$sign){
                //     $this->return['errmsg'] = '签到记录不存在';
                //     addlog('签到记录不存在');
                //     // return_json($this->return);
                // }
                // 人员
                // addlog($rfid, 'rfid');
                // $patient = Db::name('patient')->field('id,department_code,patient_id')->where(array('rfid'=>$rfid))->find();
                $patient = Db::name('patient')->field('id,department_code,patient_id')->where("rfid='{$rfid}'")->find();
                if(!$patient){
                    $this->return['errmsg'] = '病人信息不存在';
                    addlog('病人信息不存在');
                    // return_json($this->return);
                    exit;
                }
                // 设备
                $equipment = Db::name('equipment')->where(array('equipment_code'=>$addr))->find();
                if(!$equipment){
                    $this->return['errmsg'] = '设备不存在';
                    addlog('设备不存在');
                    // return_json($this->return);
                    exit;
                }
                // 区域
                $location = Db::name('location')->where(array('id'=>$equipment['location_id']))->find();
                $insertData = array(
                    'xinpian'   => $rfid,
                    'addr'      => $addr,
                    'createtime'    => time(),
                    'iswarn'    => 0
                    );
                if($patient['department_code'] == $equipment['department_code']){
                    $insertData['iswarn'] = 1;
                    // 临时外出
                    $out_record = Db::name('out_record')->where(array('patient_id'=>$patient['id']))->order("id desc")->find();
                    if($out_record){
                        if($out_record['endtime'] > time()){
                            // $this->return['errmsg'] = '临时不告警';
                            // return_json($this->return);
                            $insertData['iswarn'] = 0;
                            $insertData['equipgroup_id'] = $out_record['equipgroup_id'];
                        }
                    }
                   
                }
                Db::name('sign_record')->insertGetId($insertData);

                if( $insertData['iswarn'] == 1 ){
                    Db::name('patient')->where(array('id'=>$patient['id']))->update(array('iswarn_today'=>1));
                    $requestData = array();
                    $requestData['patient_id'] = $patient['patient_id'];
                    $requestData['department'] = $equipment['department_code'];
                    $requestData['location'] = $location['location'];
                    $requestData['rfid'] = $patient['rfid'];
                    $requestData['equipment'] = $equipment['equipment_code'];
                    addlog($requestData, 'syncWarning');
                    // $apiModel->syncWarning($requestData);
                    // $this->return['errcode'] = 0;
                    // $this->return['errmsg'] = '告警';
                    // return_json($this->return);
                }
            }
        }

        
    }

    function test_redis(){
        //连接redis数据库
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis_name = 'hospital';
        $data_len = $redis->lLen($redis_name);
        echo $data_len;
    }

    function tcp_rfid_redis(){
        $apiModel = new apiModel();
        //连接redis数据库
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis_name = 'hospital';
        $data_len = $redis->lLen($redis_name);
        if($data_len > 0){
            $data_len = min($data_len, 1000);
            for($i=0; $i<$data_len; $i++){
                //从队列最前头取出一个值
                $redis_data = $redis->lPop($redis_name);
                $signData = json_decode($redis_data, true);
                if(is_array($signData) && !empty($signData)){
                    $rfid = trim($signData['xinpian']);
                    $addr = trim($signData['addr']);
                    if(!empty($rfid) && !empty($addr)){
                        //签到
                        // 人员
                        // addlog($rfid, 'rfid');
                        // $patient = Db::name('patient')->field('id,department_code,patient_id')->where(array('rfid'=>$rfid))->find();
                        $patient = Db::name('patient')->field('id,department_code,patient_id')->where("rfid='{$rfid}'")->find();
                        if(!$patient){
                            $this->return['errmsg'] = '病人信息不存在';
                            addlog('病人信息不存在');
                            // return_json($this->return);
                            exit;
                        }
                        // 设备
                        $equipment = Db::name('equipment')->where(array('equipment_code'=>$addr))->find();
                        if(!$equipment){
                            $this->return['errmsg'] = '设备不存在';
                            addlog('设备不存在');
                            // return_json($this->return);
                            exit;
                        }
                        // 区域
                        $location = Db::name('location')->where(array('id'=>$equipment['location_id']))->find();
                        $insertData = array(
                            'xinpian'   => $rfid,
                            'addr'      => $addr,
                            'createtime'    => $signData['createtime'],
                            'iswarn'    => 0
                            );
                        if($patient['department_code'] == $equipment['department_code']){
                            $insertData['iswarn'] = 1;
                            // 临时外出
                            $out_record = Db::name('out_record')->where(array('patient_id'=>$patient['id']))->order("id desc")->find();
                            if($out_record){
                                if($out_record['endtime'] > $signData['createtime']){
                                    // $this->return['errmsg'] = '临时不告警';
                                    // return_json($this->return);
                                    $insertData['iswarn'] = 0;
                                    $insertData['equipgroup_id'] = $out_record['equipgroup_id'];
                                }
                            }
                            // 外出签到
                            $where = "status=0 AND starttime>0 AND endtime=0 AND rfid='".$rfid."'";
                            $out_sign = Db::name("out_sign")->where($where)->order("id desc")->find();
                            if($out_sign){
                                if($out_sign){
                                    $insertData['iswarn'] = 0;
                                }
                            }
                        }
                        Db::name('sign_record')->insertGetId($insertData);

                        if( $insertData['iswarn'] == 1 ){
                            Db::name('patient')->where(array('id'=>$patient['id']))->update(array('iswarn_today'=>1));
                            $requestData = array();
                            $requestData['patient_id'] = $patient['patient_id'];
                            $requestData['department'] = $equipment['department_code'];
                            $requestData['location'] = $location['location'];
                            $requestData['rfid'] = $patient['rfid'];
                            $requestData['equipment'] = $equipment['equipment_code'];
                            addlog($requestData, 'syncWarning');
                            // $apiModel->syncWarning($requestData);
                            // $this->return['errcode'] = 0;
                            // $this->return['errmsg'] = '告警';
                            // return_json($this->return);
                        }
                    }
                }
            }
        }
        //关闭redis连接
        $redis->close();

    }
    function tcp_rfid_redis2(){
        $apiModel = new apiModel();
        //连接redis数据库
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis_name = 'hospital';
        $data_len = $redis->lLen($redis_name);
        if($data_len > 0){
            $data_len = min($data_len, 1000);
            for($i=0; $i<$data_len; $i++){
                //从队列最前头取出一个值
                $data = $redis->lPop($redis_name);
                $inputData = json_decode($data, true);
                if(is_array($inputData) && !empty($inputData)){
                    foreach($inputData as $key=>$val){
                        $rfid = trim($val['xinpian']);
                        $addr = trim($val['addr']);
                        if(!empty($rfid) && !empty($addr)){
                            //签到
                            // 人员
                            // addlog($rfid, 'rfid');
                            // $patient = Db::name('patient')->field('id,department_code,patient_id')->where(array('rfid'=>$rfid))->find();
                            $patient = Db::name('patient')->field('id,department_code,patient_id')->where("rfid='{$rfid}'")->find();
                            if(!$patient){
                                $this->return['errmsg'] = '病人信息不存在';
                                addlog('病人信息不存在');
                                // return_json($this->return);
                                exit;
                            }
                            // 设备
                            $equipment = Db::name('equipment')->where(array('equipment_code'=>$addr))->find();
                            if(!$equipment){
                                $this->return['errmsg'] = '设备不存在';
                                addlog('设备不存在');
                                // return_json($this->return);
                                exit;
                            }
                            // 区域
                            $location = Db::name('location')->where(array('id'=>$equipment['location_id']))->find();
                            $insertData = array(
                                'xinpian'   => $rfid,
                                'addr'      => $addr,
                                'createtime'    => $val['createtime'],
                                'iswarn'    => 0
                                );
                            if($patient['department_code'] == $equipment['department_code']){
                                $insertData['iswarn'] = 1;
                                // 临时外出
                                $out_record = Db::name('out_record')->where(array('patient_id'=>$patient['id']))->order("id desc")->find();
                                if($out_record){
                                    if($out_record['endtime'] > $val['createtime']){
                                        // $this->return['errmsg'] = '临时不告警';
                                        // return_json($this->return);
                                        $insertData['iswarn'] = 0;
                                        $insertData['equipgroup_id'] = $out_record['equipgroup_id'];
                                    }
                                }
                               
                            }
                            Db::name('sign_record')->insertGetId($insertData);

                            if( $insertData['iswarn'] == 1 ){
                                Db::name('patient')->where(array('id'=>$patient['id']))->update(array('iswarn_today'=>1));
                                $requestData = array();
                                $requestData['patient_id'] = $patient['patient_id'];
                                $requestData['department'] = $equipment['department_code'];
                                $requestData['location'] = $location['location'];
                                $requestData['rfid'] = $patient['rfid'];
                                $requestData['equipment'] = $equipment['equipment_code'];
                                addlog($requestData, 'syncWarning');
                                // $apiModel->syncWarning($requestData);
                                // $this->return['errcode'] = 0;
                                // $this->return['errmsg'] = '告警';
                                // return_json($this->return);
                            }
                        }
                    }
                }
            }
        }

        //关闭redis连接
        $redis->close();

    }

    function warn_show(){
        $list = Db::name("sign_record")->alias("sr")
                ->leftJoin("patient p","p.rfid=sr.xinpian")
                ->leftJoin("equipment e","e.equipment_code=sr.addr")
                ->field("sr.*,p.patient_id,p.patient_name,p.department,e.equipment")
                ->where("sr.iswarn=1")
                ->order("id desc")
                ->limit("10")
                ->select();
        foreach($list as &$val){
            $val['gender'] = $val['gender']==1 ? '男' : '女';
            $val['createtime'] = date("Y-m-d H:i:s", $val['createtime']);
        }
        if(request()->isAjax()){
            exitjson(1,'请求成功',$list);
        }else{
            $this->assign('list', $list);
            return view();
        }
        

        
    }

}